package com.ciban.smarthome.module.ui.container;

import java.util.ArrayList;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.DensityUtils;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewDialog extends Dialog {
	public interface ListDialogHelper {
		public void onItemClick(int position);
	}

	private TextView tv_tilte;
	private ListView lv_content;

	private ArrayList<String> list;
	private ArrayAdapter arr_adapter;
	private ListDialogHelper listener;

	private BaseAdapter adapter;

	public void setListener(ListDialogHelper listener) {
		this.listener = listener;
	}

	public ListViewDialog(Context context, int theme) {
		super(context, theme);
		initView(context);
	}

	private void initView(Context context) {
		View view = View.inflate(context, R.layout.dialog_lvshow, null);

		LinearLayout ll_show = (LinearLayout) view
				.findViewById(R.id.lvdialog_title);
		int width = DensityUtils.getWindowWidth(context);
		LayoutParams params = new LayoutParams((int) (width * 0.65),
				LayoutParams.WRAP_CONTENT);
		ll_show.setLayoutParams(params);

		tv_tilte = (TextView) view.findViewById(R.id.common_lv_dialog_title);
		lv_content = (ListView) view
				.findViewById(R.id.common_lv_dialog_content);

		lv_content.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (listener != null) {
					listener.onItemClick(position);
				}
				dismiss();
			}
		});

		setContentView(view);
	}

	public void setList(ArrayList<String> list) {
		this.list = list;
		arr_adapter = new ArrayAdapter<String>(getContext(),
				R.layout.listitem_info,R.id.common_item_lv_desc, list);
		
//		arr_adapter
//				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		lv_content.setAdapter(arr_adapter);
	}

	public void setTitle(String title) {
		tv_tilte.setText(title);
	}

	public void setAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
		lv_content.setAdapter(adapter);
	}

}
